<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

class PublicController extends Controller
{
    //展示登录页面
    public function login()
    {
        return view('admin.public.login');
    }

    //登录验证
    public function checkLogin(Request $request)
    {
        //用户身份的验证
        //自动验证
        $this->validate($request, [
            //验证字段 => 规则|规则
            'username' => 'required|min:3|max:20',
            'password' => 'required|min:6|max:30',
            'captcha' => 'required|size:5|captcha',
        ]);
        //用户验证
        //获取用户名和密码
        $data = $request->only('username', 'password');
        $data['status'] = 2;//启用
        $res = Auth::guard('admin')->attempt($data, $request->get('online'));
        if ($res) {
            //合法用户
            return redirect('/admin/index/index');
        } else {
            //非法用户
            return redirect('/admin/public/login')->withErrors('用户名或密码错误');
        }
    }
    //退出登录
    public function logout()
    {
        Auth::guard('admin')->logout();
        return redirect('/admin/public/login');
    }
}
